package edu.bbu.commonproject.desktop.gui.view.swing.file.traffic.utils;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;

import javax.swing.Box;

import edu.bbu.commonproject.desktop.gui.view.swing.file.traffic.ListItem;
import edu.bbu.commonproject.desktop.gui.view.swing.utils.TexturedPanel;

/**
 * The list container.
 */
public class ListContainer extends TexturedPanel implements ListItemContainer
{
	/** The box. */
	private Box box;
	
	/** The selected item. */
	private ListItem selectedItem;
	
	/** The add lock. */
	private Object addLock;
	
	/**
	 * The constructor.
	 */
	public ListContainer()
	{
		box = Box.createVerticalBox();
		addLock = new Object();
		
		this.setLayout(new BorderLayout());
		this.add(box, BorderLayout.NORTH);
	}

	/**
	 * It adds the item listener.
	 * @param item the item listener.
	 */
	public void addListItem(ListItem item)
	{
		item.setListContainer(this);
		
		synchronized (addLock)
		{
			box.add(item, 0);
		}
		
		box.revalidate();
		box.repaint();
	}
	
	/**
	 * It removes the item.
	 * @param item the item.
	 */
	public void removeItem(ListItem item)
	{
		synchronized (addLock)
		{
			box.remove(item);
		}
		
		box.revalidate();
		box.repaint();
	}
	
	/**
	 * It orders the element.
	 */
	public void reorderElements()
	{
		ArrayList<ListItem> pauseds = new ArrayList<ListItem>();
		ArrayList<ListItem> uploads = new ArrayList<ListItem>();
		ArrayList<ListItem> downloads = new ArrayList<ListItem>();
		
		synchronized (addLock)
		{
			for (Component component : box.getComponents())
			{
				ListItem listItem = (ListItem) component;
				
				if(listItem.getStatus() == ListItem.DOWNLOAD)
				{
					box.remove(listItem);
					downloads.add(listItem);
				}
				else if(listItem.getStatus() == ListItem.UPLOAD)
				{
					box.remove(listItem);
					uploads.add(listItem);
				}
				else if(listItem.getStatus() == ListItem.PAUSED)
				{
					box.remove(listItem);
					pauseds.add(listItem);
				}
			}
			
			for (ListItem listItem : pauseds)
			{
				box.add(listItem, 0);
			}
			
			for (ListItem listItem : downloads)
			{
				box.add(listItem, 0);
			}
			
			for (ListItem listItem : uploads)
			{
				box.add(listItem, 0);
			}
		}
		
		box.revalidate();
		box.repaint();
	}
	
	/**
	 * It get all the item.
	 * @return all the item.
	 */
	public ListItem[] getAllItems()
	{
		ListItem[] items;
		
		synchronized (addLock)
		{
			Component[] list = box.getComponents();
			items = new ListItem[list.length];
			
			for(int i = 0; i < list.length; i++)
			{
				items[i] = (ListItem) list[i];
			}
		}
		
		return items;
	}

	/**
	 * It sets the selected object.
	 */
	public void setSelectedObject(ListItem selectedItem)
	{
		this.selectedItem = selectedItem;
	}

	/**
	 * It gets the selected object.
	 */
	public ListItem getSelectedObject()
	{
		return this.selectedItem;
	}
	
	/**
	 * It check the selected item.
	 */
	public boolean isSelectedItem(ListItem selectedItem)
	{
		return this.selectedItem == selectedItem;
	}
}
